 import Vue from 'vue'
 import axios from 'axios'
 import ls from './local.js'
 
  // 创建 axios 实例
  const service = axios.create({
    baseURL: '/jeecg-boot/', // api base_url
    timeout: 200000 // 请求超时时
  })

  const err = (error) => {
    if (error.response) {
      let data = error.response.data
      const token = ls.get('mToken')
      switch (error.response.status) {
        case 403:
          break
        case 500:
          if(token && data.message=="Token失效，请重新登录"){
			 uni.showToast({
				 title:'很抱歉，登录已过期，请重新进入登录',
				  duration: 2000
			 })
          }
          break
        // case 404:
        //    uni.showToast({ title: '',duration: 2000})
        //   break
        case 504:
          uni.showToast({ title: '网络超时', duration: 2000})
          break
        case 401:
          uni.showToast({ title: '未授权，请重新登录',duration: 2000})
          break
        default:
   
          break
      }
    }
    return Promise.reject(error)
  };
  
  service.interceptors.request.use(config => {
    const token = ls.get('mToken')
    if (token) {
      config.headers[ 'X-Access-Token' ] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
    }
    if(config.method=='get'){
      config.params = {
        _t: Date.parse(new Date())/1000,
        ...config.params
      }
    }
    return config
  },(error) => {
    return Promise.reject(error)
  })
  
  // response interceptor
  service.interceptors.response.use((response) => {
      return response.data
    }, err)
	
	export {
		service as axios
	}

   